home *** CD-ROM | disk | FTP | other *** search
/ SGI Hot Mix 17 / Hot Mix 17.iso / HM17_SGI / research / lib / loadct.pro < prev    next >
Text File  |  1997-07-08  |  5KB  |  178 lines

  1. ; $Id: loadct.pro,v 1.7 1997/01/15 03:11:50 ali Exp $
  2. ;
  3. ; Copyright (c) 1982-1997, Research Systems, Inc.  All rights reserved.
  4. ;       Unauthorized reproduction prohibited.
  5.  
  6. PRO loadct, table_number, SILENT = silent, GET_NAMES = rnames, FILE=file, $
  7.     NCOLORS = nc1, BOTTOM=bottom
  8. ;+
  9. ; NAME:
  10. ;    LOADCT
  11. ;
  12. ; PURPOSE:
  13. ;    Load predefined color tables.
  14. ;
  15. ; CATEGORY:
  16. ;    Image display.
  17. ;
  18. ; CALLING SEQUENCE:
  19. ;    LOADCT [, Table]
  20. ;
  21. ; OPTIONAL INPUTS:
  22. ;    Table:    The number of the pre-defined color table to load, from 0 
  23. ;        to 15.  If this value is omitted, a menu of the available 
  24. ;        tables is printed and the user is prompted to enter a table 
  25. ;        number.
  26. ;
  27. ; KEYWORD PARAMETERS:
  28. ;    FILE:    If this keyword is set, the file by the given name is used
  29. ;        instead of the file colors1.tbl in the IDL directory.  This
  30. ;        allows multiple IDL users to have their own color table file.
  31. ;        The specified file must exist.
  32. ;    GET_NAMES: If this keyword is present AND DEFINED, the names
  33. ;        of the color tables are returned as a string array.
  34. ;        No changes are made to the color table.
  35. ;    NCOLORS = number of colors to use.  Use color indices from 0
  36. ;        to the smaller of !D.TABLE_SIZE-1 and NCOLORS-1.
  37. ;        Default = !D.TABLE_SIZE = all available colors.
  38. ;    SILENT:    If this keyword is set, the Color Table message is suppressed.
  39. ;    BOTTOM = first color index to use. Use color indices from BOTTOM to
  40. ;        BOTTOM+NCOLORS-1.  Default = 0.
  41. ;
  42. ; OUTPUTS:
  43. ;    No explicit outputs.
  44. ;
  45. ; COMMON BLOCKS:
  46. ;    COLORS:    The IDL color common block.
  47. ;
  48. ; SIDE EFFECTS:
  49. ;    The color tables of the currently-selected device are modified.
  50. ;
  51. ; RESTRICTIONS:
  52. ;    Works from the file: $IDL_DIR/resource/colors/colors1.tbl or the file specified
  53. ;    with the FILE keyword.
  54. ;
  55. ; PROCEDURE:
  56. ;    The file "colors1.tbl" or the user-supplied file is read.  If
  57. ;       the currently selected device doesn't have 256 colors, the color
  58. ;    data is interpolated from 256 colors to the number of colors
  59. ;    available.
  60. ;
  61. ;    The colors loaded into the display are saved in the common
  62. ;    block COLORS, as both the current and original color vectors.
  63. ;
  64. ;    Interpolation:  If the current device has less than 256 colors,
  65. ;    the color table data is interpolated to cover the number of
  66. ;    colors in the device.
  67. ;
  68. ; MODIFICATION HISTORY:
  69. ;    Old.  For a widgetized version of this routine, see XLOADCT in the IDL
  70. ;        widget library.
  71. ;    DMS, 7/92, Added new color table format providing for more than
  72. ;        16 tables.  Now uses file colors1.tbl.  Old LOADCT procedure
  73. ;        is now OLD_LOADCT.
  74. ;    ACY, 9/92, Make a pixmap if no windows exist for X windows to
  75. ;        determine properly the number of available colors.
  76. ;        Add FILE keyword.
  77. ;    WSO, 1/95, Updated for new directory structure
  78. ;    AB, 10/3/95, The number of entries in the COLORS common block is
  79. ;        now always !D.TABLE_SIZE instead of NCOLORS + BOTTOM as
  80. ;        before. This better reflects the true state of the device and
  81. ;        works with other color manipulations routines.
  82. ;
  83. ;-
  84. common colors, r_orig, g_orig, b_orig, r_curr, g_curr, b_curr
  85.  
  86.  
  87. on_ioerror, bad
  88. on_error, 2        ;Return to caller if error
  89. get_lun, lun
  90.  
  91. if !d.name eq 'X' and !d.window eq -1 then begin  ;Uninitialized?
  92. ;    If so, make a dummy window to determine the # of colors available.
  93.     window,/free,/pixmap,xs=4, ys=4
  94.     wdelete, !d.window
  95.     endif
  96.  
  97. if n_elements(bottom) gt 0 then cbot = bottom > 0 < (!D.TABLE_SIZE-1) $
  98.     else cbot = 0
  99. nc = !d.table_size - cbot
  100. if n_elements(nc1) gt 0 then nc = nc < nc1
  101.  
  102. if nc eq 0 then message, 'Device has static color tables.  Can''t load.'
  103.  
  104. if (n_elements(file) GT 0) then filename = file $
  105. else filename = filepath('colors1.tbl', subdir=['resource', 'colors'])
  106.  
  107. openr,lun, filename, /block
  108.  
  109. ntables = 0b
  110. readu, lun, ntables
  111.  
  112. IF    (n_params() eq 0) or $        ;Read names?
  113.     (n_elements(rnames) ge 1) or $
  114.     (not keyword_set(silent)) then begin
  115.         names = bytarr(32, ntables)
  116.         point_lun, lun, ntables * 768L + 1    ;Read table names
  117.         readu, lun, names
  118.         names = strtrim(names, 2)
  119.     ENDIF
  120.  
  121. IF n_elements(rnames) ge 1 THEN BEGIN    ;Return names?
  122.     rnames = names
  123.     goto, close_file
  124.     ENDIF
  125.  
  126.  
  127. if n_params() lt 1 then begin    ;Summarize table?
  128.     nlines = (ntables + 2) / 3    ;# of lines to print
  129.     for i=0, nlines-1 do $        ;Print each line
  130.       print, format="(i2,'- ',a17, 3x, i2,'- ',a17, 3x, i2,'- ',a17)", $
  131.         i, names[i], i+nlines, names[i+nlines], i+2*nlines < (ntables-1), $
  132.         names[i+2*nlines < (ntables-1)]
  133.     read,'Enter table number: ',table_number
  134.     endif
  135.  
  136. if (table_number ge ntables) or (table_number lt 0) then begin
  137.   message, 'Table number must be from 0 to ' + strtrim(ntables-1, 2)
  138.   end
  139.  
  140.  
  141. if n_elements(r_orig) lt !d.table_size then begin    ;Tables defined?
  142.     r_orig = BYTSCL(indgen(!d.table_size))
  143.     g_orig = r_orig
  144.     b_orig = r_orig
  145.     endif
  146.  
  147.  
  148. if keyword_set(silent) eq 0 then $
  149.     message,'Loading table ' + names[table_number],/INFO
  150. aa=assoc(lun, bytarr(256),1)    ;Read 256 long ints
  151. r = aa[table_number*3]
  152. g = aa[table_number*3+1]
  153. b = aa[table_number*3+2]
  154.  
  155. if nc ne 256 then begin    ;Interpolate
  156.     p = (lindgen(nc) * 255) / (nc-1)
  157.     r = r[p]
  158.     g = g[p]
  159.     b = b[p]
  160.     endif
  161.  
  162. r_orig[cbot] = r
  163. g_orig[cbot] = g
  164. b_orig[cbot] = b
  165. r_curr = r_orig
  166. g_curr = g_orig
  167. b_curr = b_orig
  168. tvlct,r, g, b, cbot
  169. goto, close_file
  170.  
  171. bad:
  172.   message,'Error reading file: ' + filename + ', ' + !err_string
  173.  
  174. close_file:
  175.   free_lun,lun
  176.   return
  177. end
  178.